﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShopOnline.Models.DAL;
using ShopOnline.Models.Utils;
using ShopOnline.Models;

namespace ShopOnline.Controllers
{
    public class ProductController : Controller
    {
        private ShopOnline.Models.ShopOnlineDBDataContext db;
        private DAL_Product ProductRepository;
        private DAL_ProductCatelogy ProductCatelogyRepository;
        private static int productcurent;
        public static int productCateId;
        public static int Productcurent
        {
            get { return ProductController.productcurent; }
            set { ProductController.productcurent = value; }
        }

        public ProductController()
        {
            db = new Models.ShopOnlineDBDataContext();
            ProductRepository = new DAL_Product();
            ProductCatelogyRepository = new DAL_ProductCatelogy();
        }

        //
        // GET: /Product/
         [Authorize]
        public ActionResult AdminListProduct()
        {
            var list = ProductRepository.getListProduct();
            return View(list);
        }

        //
        // GET: /Product/Details/5
         [Authorize]
        public ActionResult Details(int id)
        {
            var product = ProductRepository.getProductById(id);
            var list = ProductCatelogyRepository.getListProductCatelogyIsShow();
            ViewData["listProductCatelogy"] = list;
            return View(product);
        }

        //
        // GET: /Product/Create
         [Authorize]
        public ActionResult Create()
        {
            var list = ProductCatelogyRepository.getListProductCatelogyIsShow();
            ViewData["listProductCatelogy"] = list;
            return View();
        } 

        //
        // POST: /Product/Create
         [Authorize]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                Product product = new Product();
                product.description = collection["description"];
                product.information = collection["information"];
                product.piority = int.Parse(collection["piority"].Trim());
                product.productCatelogyId = int.Parse(collection["catelogyId"].Trim());
                product.productName = collection["productName"];
                product.QuatityPerUnits = collection["QuatityPerUnits"];
                var show = collection["show"];
                if (show == "false")
                {
                    product.show = false;
                }
                else
                {
                    product.show = true;
                }
                var showAdvert = collection["showAdvert"];
                if (showAdvert == "false")
                {
                    product.showAdvert = false;
                }
                else
                {
                    product.showAdvert = true;
                }
                product.unitInStock = 0;
                product.unitPrice = 0;
                product.unitsOnOrder = 0;
                #region upload images
                Uploader uploader = new Uploader();
                uploader.UploadPath = Server.MapPath("~\\Content\\Products");
                uploader.IsLowerName = false;
                uploader.IsEncryptName = false;
                uploader.IsOverwrite = false;
                uploader.Prefix = "";
                uploader.Suffix = "";

                uploader.AllowedExtensions.Add(".jpg");
                uploader.AllowedExtensions.Add(".jpeg");
                uploader.AllowedExtensions.Add(".gif");
                uploader.AllowedExtensions.Add(".png");

                bool success = uploader.DoUpload("MyFile");
                #endregion
                product.image = uploader.PostedFile.FileName;
                db.Products_add(product.productName, product.description, product.information, product.unitPrice, product.unitInStock, product.unitsOnOrder, product.QuatityPerUnits, product.image, product.piority, product.show, product.showAdvert,product.productCatelogyId);
                // TODO: Add insert logic here

                return RedirectToAction("AdminListProduct");
            }
            catch
            {
                var list = ProductCatelogyRepository.getListProductCatelogyIsShow();
                ViewData["listProductCatelogy"] = list;
                return View();
            }
        }
        
        //
        // GET: /Product/Edit/5
  [Authorize]
        public ActionResult Edit(int id)
        {
            var list = ProductCatelogyRepository.getListProductCatelogyIsShow();
            ViewData["listProductCatelogy"] = list;
            var product = ProductRepository.getProductById(id);
            return View(product);
        }

        //
        // POST: /Product/Edit/5
         [Authorize]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                Product product = ProductRepository.getProductById(id);
                product.description = collection["description"];
                product.information = collection["information"];
                product.piority = int.Parse(collection["piority"].Trim());
                product.productCatelogyId = int.Parse(collection["catelogyId"].Trim());
                product.productName = collection["productName"];
                product.QuatityPerUnits = collection["QuatityPerUnits"];
                var show = collection["show"];
                if (show == "false")
                {
                    product.show = false;
                }
                else
                {
                    product.show = true;
                }
                var showAdvert = collection["showAdvert"];
                if (showAdvert == "false")
                {
                    product.showAdvert = false;
                }
                else
                {
                    product.showAdvert = true;
                }
                product.unitInStock = 0;
                product.unitPrice = 0;
                product.unitsOnOrder = 0;

                db.Products_update(product.productId, product.productName, product.description, product.information, product.unitPrice, product.unitInStock, product.unitsOnOrder, product.QuatityPerUnits, product.image, product.piority, product.show, product.showAdvert, product.productCatelogyId);
                return RedirectToAction("AdminListProduct");
            }
            catch
            {
                var list = ProductCatelogyRepository.getListProductCatelogyIsShow();
                ViewData["listProductCatelogy"] = list;
                var product = ProductRepository.getProductById(id);
                return View(product);
            }
        }

        //
        // GET: /Product/Delete/5
  [Authorize]
        public ActionResult Delete(int id)
        {
            var product = ProductRepository.getProductById(id);
            return View(product);
        }

        //
        // POST: /Product/Delete/5
 [Authorize]
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                var product = ProductRepository.getProductById(id);
                ProductRepository.DeleteProduct(product);
                return RedirectToAction("AdminListProduct");
            }
            catch
            {
                var product = ProductRepository.getProductById(id);
                return View(product);
            }
        }

        public ActionResult EditImage(int id)
        {
            try
            {
                productcurent = id;
                var pro = ProductRepository.getProductById(id);
                return View(pro);
            }
            catch (Exception)
            {
                return RedirectToAction("AdminListProduct");
            }

        }
 [Authorize]
        [HttpPost]
        public ActionResult EditImages()
        {
            try
            {
                var product = ProductRepository.getProductById(productcurent);
                #region upload images
                Uploader uploader = new Uploader();
                uploader.UploadPath = Server.MapPath("~\\Content\\Products");
                uploader.IsLowerName = false;
                uploader.IsEncryptName = false;
                uploader.IsOverwrite = false;
                uploader.Prefix = "";
                uploader.Suffix = "";

                uploader.AllowedExtensions.Add(".jpg");
                uploader.AllowedExtensions.Add(".jpeg");
                uploader.AllowedExtensions.Add(".gif");
                uploader.AllowedExtensions.Add(".png");

                bool success = uploader.DoUpload("MyFile");
                #endregion
                string abc = uploader.PostedFile.FileName;
                db.Products_update(product.productId, product.productName, product.description, product.information, product.unitPrice, product.unitInStock, product.unitsOnOrder, product.QuatityPerUnits, abc, product.piority, product.show, product.showAdvert, product.productCatelogyId);
                return RedirectToAction("Edit", new { id = productcurent });
            }
            catch (Exception)
            {
                return RedirectToAction("EditImage", new { id = productcurent });
            }

        }

        public ActionResult ViewDetail(int? n, int? cateId)
        {
           int a=n??0;
           int catedefaut =cateId?? ProductRepository.getCatelogyIdDefaut();
           List<Product> listProduct = ProductRepository.get6Product(a, 6, catedefaut);
           int count = ProductRepository.getListProduct().Count();
           ViewData["count"] = count;
           ViewData["cateId"] = catedefaut;
           return View(listProduct);
        }

        public ActionResult ListProduct()
        {
            var listproduct = ProductRepository.getListProduct();
            ViewData["listproduct"] = listproduct;
            return View();

        }
        public ActionResult ListProductByCate(int cateId)
        {
            var productCate = ProductCatelogyRepository.getProductCatelogyById(cateId);
            var listProductByCate = ProductRepository.getListProductByCateId(cateId);
            ViewData["listProductByCate"] = listProductByCate;
            ViewData["productCate"] = productCate.catelogyName;
            productCateId = int.Parse( productCate.catelogyId.ToString());
            return View();
        }
        public ActionResult ListDetail(int id)
        {
            var listDetail = ProductRepository.getProductById(id);
            var listLinkOrther = ProductRepository.getList6Orther(id);
            ViewData["listLinkOrther"] = listLinkOrther;
            /*
            var productCate = ProductCatelogyRepository.getProductCatelogyById(productCateId);
            ViewData["productCateName"] = productCate.catelogyName;*/
            return View(listDetail);
        }
    }
}
